package com.suke.money.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.suke.money.mapper.BidInfoMapper;
import com.suke.money.mapper.IncomeRecordMapper;
import com.suke.money.mapper.LoanInfoMapper;
import com.suke.money.model.BidInfo;
import com.suke.money.model.IncomeRecord;
import com.suke.money.model.LoanInfo;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

/**
 * @Author: liush
 * @CreateTime: 2022-12-30  18:26
 * @Description: 收益业务实现类
 */
@Service(interfaceClass = IncomeRecordService.class, version = "1.0.0", timeout = 20000)
@Component
public class IncomeRecordServiceImpl implements IncomeRecordService {

    @Autowired
    LoanInfoMapper loanInfoMapper;

    @Autowired
    BidInfoMapper bidInfoMapper;

    @Autowired
    IncomeRecordMapper incomeRecordMapper;

    /*
     * 生成收益计划：
     * 1.查询满标的产品(List)
     * 2.遍历满标产品
     * 3.根据产品编号查询投资记录(List)
     * 4.遍历投资记录
     * 5.生成收益计划(计算利息和时间)
     * 6.修改产品状态为2(满标并产生收益计划为2)
     * */
    @Override
    @Transactional
    public void generatePlan() {
        //1.查询满标的产品(List)
        List<LoanInfo> loanInfos = loanInfoMapper.selectLoanInfosByFullStatus();
        //2.遍历满标产品
        for (LoanInfo loanInfo : loanInfos) {
            //3.根据产品编号查询投资记录(List)
            List<BidInfo> bidInfos = bidInfoMapper.selectBidInfosByLoanId(loanInfo.getId());
            //4.遍历投资记录
            for (BidInfo bidInfo : bidInfos) {
                //5.生成收益计划(计算利息和时间)
                IncomeRecord incomeRecord = new IncomeRecord();
                incomeRecord.setBidId(bidInfo.getId());
                incomeRecord.setBidMoney(bidInfo.getBidMoney());
                incomeRecord.setIncomeStatus(0);
                incomeRecord.setLoanId(bidInfo.getLoanId());
                incomeRecord.setUid(bidInfo.getUid());

                Date date = null;
                Double income; //income:收益
                if (loanInfo.getProductType() == 0) {
                    date = DateUtils.addDays(loanInfo.getProductFullTime(), loanInfo.getCycle());
                    income = loanInfo.getRate() / 100 / 365 * loanInfo.getCycle() * bidInfo.getBidMoney();
                } else {
                    date = DateUtils.addMonths(loanInfo.getProductFullTime(), loanInfo.getCycle());
                    income = loanInfo.getRate() / 100 / 365 * loanInfo.getCycle() * 30 * bidInfo.getBidMoney();
                }
                incomeRecord.setIncomeDate(date);
                //四舍五入保留2位小数
                BigDecimal bigDecimal = new BigDecimal(income).setScale(2, RoundingMode.UP);
                incomeRecord.setIncomeMoney(bigDecimal.doubleValue());
                incomeRecordMapper.insertSelective(incomeRecord);
            }
            //6.修改产品状态为2
            loanInfo.setProductStatus(2);
            loanInfoMapper.updateByPrimaryKeySelective(loanInfo);
        }
    }

}
